package com.library.bookinfo.service;

import com.library.bookinfo.mapper.BookinfoMapper;
import com.library.bookinfo.pojo.BookInfo;
import com.library.common.util.UUIDUtil;
import io.netty.util.internal.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.library.bookinfo.ExcelUnscramble.ExcelAnalyse;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.util.*;

@Service
public class BookinfoService {
    private static final Logger logger = LoggerFactory.getLogger(BookinfoService.class);

    @Autowired
    private BookinfoMapper bookinfoMapper;

    public String InsertFromExcelFile(String filePath){
        Long startTime=System.currentTimeMillis();
        String columns[] = {"bookName", "bookAuthor", "bookPress", "bookPublishtime", "bookPrice", "imgUrl", "ISBN", "bookBrief", "comment", "pName", "bookCategoryid", "bookCategoryid1"};

        List bList=ExcelAnalyse.getExcelReadList(filePath,columns);
        List dealedBookList=new LinkedList();

        for(Object bookMap:bList) {
            Map<String, String> bm = (Map) bookMap;
            // bookId,bookName,bookCategoryid, ISBN, bookNo,
            //       bookAuthor,bookBrief,bookPrice,bookPress,bookStorage,
            //       bookPublishtime,bookStatus
            BookInfo bookInfo = new BookInfo();
            bookInfo.setBookId(UUIDUtil.getUUID());
            bookInfo.setBookName(StringUtil.isNullOrEmpty(bm.get("bookName"))?"":bm.get("bookName"));
            bookInfo.setBookCategoryid(StringUtil.isNullOrEmpty(bm.get("bookCategoryid"))?"":bm.get("bookCategoryid"));
            bookInfo.setISBN(StringUtil.isNullOrEmpty(bm.get("ISBN"))?"":bm.get("ISBN").replace("ISBN","").replace("：",""));
            bookInfo.setBookNo(1);
            bookInfo.setBookBrief(StringUtil.isNullOrEmpty(bm.get("bookBrief"))?"":bm.get("bookBrief"));
            bookInfo.setBookPrice(Double.parseDouble(StringUtil.isNullOrEmpty(bm.get("bookPrice"))?"":bm.get("bookPrice").replace("¥","")));
            bookInfo.setBookPress(StringUtil.isNullOrEmpty(bm.get("bookPress"))?"":bm.get("bookPress"));
            bookInfo.setBookStorage(StringUtil.isNullOrEmpty(bm.get("bookStorage"))?"":bm.get("bookStorage"));
           // bi.setBookPublishtime(new Date(StringUtil.isNullOrEmpty(bm.get("bookPublishtime"))?"":bm.get("bookPublishtime")));
            bookInfo.setBookStatus(0);

            dealedBookList.add(bookInfo);
        }
        bookinfoMapper.insertManyBook(dealedBookList);

        Long endTime=System.currentTimeMillis();

        return endTime-startTime+"";
    }

    public String InsertFromExcelFile1(String filePath) throws UnsupportedEncodingException {
        Long startTime=System.currentTimeMillis();
        String columns[] = {"bookName", "bookAuthor", "bookPress", "bookPublishtime", "bookPrice", "imgUrl", "ISBN", "bookBrief", "comment", "pName", "bookCategoryid", "bookCategoryid1"};
        List bList=ExcelAnalyse.getExcelReadList(filePath,columns);
        List dealedBookList=new LinkedList();

        for(Object bookMap:bList) {
            Map<String, String> bm = (Map) bookMap;
            // bookId,bookName,bookCategoryid, ISBN, bookNo,
            //       bookAuthor,bookBrief,bookPrice,bookPress,bookStorage,
            //       bookPublishtime,bookStatus
            BookInfo bookInfo = new BookInfo();
//            bookInfo.setBookId(UUIDUtil.getUUID());
//            bookInfo.setBookName(StringUtil.isNullOrEmpty(bm.get("bookName"))?"":
//                    new String(bm.get("bookName").getBytes(),"UTF-8"));
//            bookInfo.setBookCategoryid(StringUtil.isNullOrEmpty(bm.get("bookCategoryid"))?"":
//                    new String(bm.get("bookCategoryid").getBytes(),"UTF-8"));
//            bookInfo.setISBN(StringUtil.isNullOrEmpty(bm.get("ISBN"))?"":
//                    new String(bm.get("ISBN").getBytes(),"UTF-8"));
//            bookInfo.setBookNo(1);
//            bookInfo.setBookBrief(StringUtil.isNullOrEmpty(bm.get("bookBrief"))?"":
//                    new String(bm.get("bookBrief").getBytes(),"UTF-8"));
//            bookInfo.setBookPrice(Double.parseDouble(StringUtil.isNullOrEmpty(bm.get("bookPrice"))?"":
//                    new String(bm.get("bookPrice").replace("¥","").getBytes(),"UTF-8")));
//            bookInfo.setBookPress(StringUtil.isNullOrEmpty(bm.get("bookPress"))?"":
//                    new String(bm.get("bookPress").getBytes(),"UTF-8"));
//            bookInfo.setBookStorage(StringUtil.isNullOrEmpty(bm.get("bookStorage"))?"":
//                    new String(bm.get("bookStorage").getBytes(),"UTF-8"));
//            // bi.setBookPublishtime(new Date(StringUtil.isNullOrEmpty(bm.get("bookPublishtime"))?"":bm.get("bookPublishtime")));
//            bookInfo.setBookStatus(0);
            bookInfo.setBookId(UUIDUtil.getUUID());
            bookInfo.setBookName(StringUtil.isNullOrEmpty(bm.get("bookName"))?"":bm.get("bookName"));
            bookInfo.setBookCategoryid(StringUtil.isNullOrEmpty(bm.get("bookCategoryid"))?"":bm.get("bookCategoryid"));
            bookInfo.setISBN(StringUtil.isNullOrEmpty(bm.get("ISBN"))?"":bm.get("ISBN"));
            bookInfo.setBookNo(1);
            bookInfo.setBookBrief(StringUtil.isNullOrEmpty(bm.get("bookBrief"))?"":bm.get("bookBrief"));
            bookInfo.setBookPrice(Double.parseDouble(StringUtil.isNullOrEmpty(bm.get("bookPrice"))?"":bm.get("bookPrice").replace("¥","")));
            bookInfo.setBookPress(StringUtil.isNullOrEmpty(bm.get("bookPress"))?"":bm.get("bookPress"));
            bookInfo.setBookStorage(StringUtil.isNullOrEmpty(bm.get("bookStorage"))?"":bm.get("bookStorage"));
            // bi.setBookPublishtime(new Date(StringUtil.isNullOrEmpty(bm.get("bookPublishtime"))?"":bm.get("bookPublishtime")));
            bookInfo.setBookStatus(0);

            bookinfoMapper.insertOneBook(bookInfo);
        }
        Long endTime=System.currentTimeMillis();

        return endTime-startTime+"";

    }




}
